package com.example.drive.admin.service.impl;

import com.example.drive.admin.mapper.AdminMapper;
import com.example.drive.admin.pojo.dto.StudentDTO;
import com.example.drive.admin.pojo.dto.UpdatePwdDTO;
import com.example.drive.admin.pojo.dto.UserDTO;
import com.example.drive.admin.pojo.entity.Admin;
import com.example.drive.audit.pojo.entity.Audit;
import com.example.drive.admin.pojo.entity.Detail;
import com.example.drive.car.mapper.CarMapper;
import com.example.drive.car.pojo.Car;
import com.example.drive.coach.pojo.entity.Coach;
import com.example.drive.student.pojo.entity.Student;
import com.example.drive.admin.pojo.vo.UserLoginVO;
import com.example.drive.admin.service.AdminService;
import com.example.drive.coach.mapper.CoachMapper;
import com.example.drive.common.*;
import com.example.drive.student.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private CarMapper carMapper;

    /**
     * 登录
     * @param userDTO
     */
    @Override
    public UserLoginVO login(UserDTO userDTO){
        //系统管理员
    if(userDTO.getType().equals(0)){
        Admin admin = adminMapper.getByName(userDTO.getUsername());
        if(admin==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
        }
        if(!admin.getPassword().equals(userDTO.getPassword())){
            throw new ServiceException(ServiceCode.ERR_PWD,"用户或密码错误！");
        }
        // 从user中取出需要的数据：id,username,封装
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(admin.getId());
        userLoginVO.setUsername(admin.getUsername());
        userLoginVO.setType(0);
        // 生成token
        String token = JWTUtils.generateToken(userLoginVO);
        userLoginVO.setToken(token);
        return userLoginVO;
    }
    //审核员
        if(userDTO.getType().equals(1)){
            Audit audit = adminMapper.getByAuName(userDTO.getUsername());
            if(audit == null){
                throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
            }
            if(!audit.getPassword().equals(userDTO.getPassword())){
                throw new ServiceException(ServiceCode.ERR_PWD,"用户或密码错误！");
            }
            // 从user中取出需要的数据：id,username,封装
            UserLoginVO userLoginVO = new UserLoginVO();
            userLoginVO.setId(audit.getId());
            userLoginVO.setUsername(audit.getUsername());
            userLoginVO.setType(1);
            // 生成token
            String token = JWTUtils.generateToken(userLoginVO);
            userLoginVO.setToken(token);
            return userLoginVO;
        }
        //学员
        if(userDTO.getType().equals(2)){
            Student student = adminMapper.getByStuName(userDTO.getUsername());
            if(student==null){
                throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
            }
            String password = MD5Utils.enctype(userDTO.getPassword(), student.getSalt(), Constants.HASH_TIME);
            if (!password.equals(student.getPassword())) {
                throw new ServiceException(ServiceCode.ERR_PWD, "密码错误");
            }
            // 从user中取出需要的数据：id,username,封装
            UserLoginVO userLoginVO = new UserLoginVO();
            userLoginVO.setId(student.getId());
            userLoginVO.setUsername(student.getUsername());
            userLoginVO.setType(2);
            // 生成token
            String token = JWTUtils.generateToken(userLoginVO);
            userLoginVO.setToken(token);
            return userLoginVO;
        }

        return null;
    }

    /**
     * 注册学员
     * @param student
     */
    @Override
    public void register(StudentDTO student){
        Student student1=new Student();
        student1.setUsername(student.getUsername());
        String salt = UUID.randomUUID().toString().replace("-", "");
        student1.setSalt(salt);
        String password = MD5Utils.enctype(student.getPassword(), salt, Constants.HASH_TIME);
        student1.setPassword(password);
        student1.setReflag(0);
        student1.setKm1(-1);
        student1.setKm2(-1);
        student1.setKm3(-1);
        student1.setKm4(-1);
       int result = adminMapper.register(student1);
       if(result<=0){
         throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"注册失败！");
     }
    }

    /**
     * 更改密码
     * @param updatePwdDTO
     * @param userLoginVO
     */
    @Override
    public void change(UpdatePwdDTO updatePwdDTO, UserLoginVO userLoginVO){
        Admin admin = adminMapper.getByName(userLoginVO.getUsername());
        if(admin==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该用户！");
        }
        if(!updatePwdDTO.getOldPwd().equals(admin.getPassword())){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"密码不一致！");
        }
        admin.setPassword(updatePwdDTO.getNewPwd());
        int result = adminMapper.update(admin);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败!");
        }
    }

    /**
     * 驾校总业绩数据
     * @param id
     * @return
     */
    @Override
    public Detail getDetail(Integer id){
        Detail detail = adminMapper.getDetail(id);
        if(detail==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该数据！");
        }
        //学生人数
        List<Student> studentList = studentMapper.getList();
        detail.setStu_num(studentList.size());
        //驾校收入
        Integer income=2800*studentList.size();
        detail.setIncome(income);
        //教练人数
        List<Coach> coachList = coachMapper.getList();
        detail.setCo_num(coachList.size());
        //架校支出
        Integer outcome=0;
        for(Coach coach:coachList){
            outcome+=coach.getWage();
        }
        detail.setOutcome(outcome);
        List<Car> carList = carMapper.getList();
        detail.setCar_num(carList.size());
        Integer km1=0,km2=0,km3=0,km4=0,km=0;
        for(Student student: studentList){
            //未通过考试
            if(student.getKm1()<90){
                km++;
            }
            //只通过科一
            if(student.getKm1()>89 && student.getKm2()<80 && student.getKm3()<90){
                km1++;
            }
            //科一基础上，通过科二
            if(student.getKm2()>79 && student.getKm4()<90){
                km2++;
            }
            //科一基础上，通过科三
            if(student.getKm3()>89 && student.getKm4()<90){
                km3++;
            }
            //通过所有考试
            if(student.getKm4()>89){
                km4++;
            }
        }
        detail.setNoPass(km);
        detail.setPassKm1(km1);
        detail.setPassKm2(km2);
        detail.setPassKm3(km3);
        detail.setPassKm4(km4);
        int result = adminMapper.saveDetail(detail);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"保存失败！");
        }
        return detail;
    }

}
